home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / lib / emacs / 19.22 / lisp / iso-acc.el < prev    next >
Lisp/Scheme  |  1993-11-21  |  8KB  |  252 lines

  1. ;;; iso-acc.el -- minor mode providing electric accent keys
  2. ;;; Copyright (C) 1993 Free Software Foundation, Inc.
  3.  
  4. ;; Author: Johan Vromans <jv@mh.nl>
  5. ;; Version: 1.7 (modified)
  6. ;; Maintainer: FSF
  7. ;; Keywords: i18n
  8.  
  9. ;; This file is part of GNU Emacs.
  10.  
  11. ;; GNU Emacs is free software; you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation; either version 2, or (at your option)
  14. ;; any later version.
  15.  
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. ;; GNU General Public License for more details.
  20.  
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  23. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  24.  
  25. ;;; Commentary:
  26.  
  27. ;; Function `iso-accents-mode' activates a minor mode
  28. ;; (`iso-accents-minor-mode') in which typewriter "dead keys" are
  29. ;; emulated.  The purpose of this emulation is to provide a simple
  30. ;; means for inserting accented characters according to the ISO-8859-1
  31. ;; character set.
  32. ;;
  33. ;; In `iso-accents-minor-mode', pseudo accent characters are used to
  34. ;; introduce accented keys.  The pseudo-accent characterss are:
  35. ;;
  36. ;;   '  (minute)    -> grave accent
  37. ;;   `  (backtick)  -> acute accent
  38. ;;   "  (second)    -> diaeresis
  39. ;;   ^  (caret)     -> circumflex
  40. ;;   ~  (tilde)     -> tilde over the character
  41. ;;   /  (slash)     -> slash through the character.
  42. ;;                  Also:  /A is A-with-ring and /E is AE ligature.
  43. ;;
  44. ;; The action taken depends on the key that follows the pseudo accent.
  45. ;; In general: 
  46. ;;
  47. ;;   pseudo-accent + appropriate letter -> accented letter
  48. ;;   pseudo-accent + space -> pseudo-accent
  49. ;;   pseudo-accent + pseudo-accent -> accent (if available)
  50. ;;   pseudo-accent + other -> pseudo-accent + other
  51. ;;
  52. ;; If the pseudo-accent is followed by anything else than a 
  53. ;; self-insert-command, the dead-key code is terminated, the
  54. ;; pseudo-accent inserted 'as is' and the bell is rung to signal this.
  55. ;;
  56. ;; Function `iso-accents-mode' can be used to enable the iso accents
  57. ;; minor mode, or disable it.
  58.  
  59. ;; If you want only some of these characters to serve as accents,
  60. ;; set iso-accents-enable to the list of characters that should be special.
  61.  
  62. ;;; Code:
  63.  
  64. (provide 'iso-acc)
  65.  
  66. (defvar iso-accents-list
  67.   '(((?' ?A) ?\301)
  68.     ((?' ?E) ?\311)
  69.     ((?' ?I) ?\315)
  70.     ((?' ?O) ?\323)
  71.     ((?' ?U) ?\332)
  72.     ((?' ?Y) ?\335)
  73.     ((?' ?a) ?\341)
  74.     ((?' ?e) ?\351)
  75.     ((?' ?i) ?\355)
  76.     ((?' ?o) ?\363)
  77.     ((?' ?u) ?\372)
  78.     ((?' ?y) ?\375)
  79.     ((?' ?') ?\264)
  80.     ((?' ? ) ?')
  81.     ((?` ?A) ?\300)
  82.     ((?` ?E) ?\310)
  83.     ((?` ?I) ?\314)
  84.     ((?` ?O) ?\322)
  85.     ((?` ?U) ?\331)
  86.     ((?` ?a) ?\340)
  87.     ((?` ?e) ?\350)
  88.     ((?` ?i) ?\354)
  89.     ((?` ?o) ?\362)
  90.     ((?` ?u) ?\371)
  91.     ((?` ? ) ?`)
  92.     ((?` ?`) ?`)        ; no special code?
  93.     ((?^ ?A) ?\302)
  94.     ((?^ ?E) ?\312)
  95.     ((?^ ?I) ?\316)
  96.     ((?^ ?O) ?\324)
  97.     ((?^ ?U) ?\333)
  98.     ((?^ ?a) ?\342)
  99.     ((?^ ?e) ?\352)
  100.     ((?^ ?i) ?\356)
  101.     ((?^ ?o) ?\364)
  102.     ((?^ ?u) ?\373)
  103.     ((?^ ? ) ?^)
  104.     ((?^ ?^) ?^)        ; no special code?
  105.     ((?\" ?A) ?\304)
  106.     ((?\" ?E) ?\313)
  107.     ((?\" ?I) ?\317)
  108.     ((?\" ?O) ?\326)
  109.     ((?\" ?U) ?\334)
  110.     ((?\" ?a) ?\344)
  111.     ((?\" ?e) ?\353)
  112.     ((?\" ?i) ?\357)
  113.     ((?\" ?o) ?\366)
  114.     ((?\" ?s) ?\337)
  115.     ((?\" ?u) ?\374)
  116.     ((?\" ?y) ?\377)
  117.     ((?\" ? ) ?\")
  118.     ((?\" ?\") ?\250)
  119.     ((?\~ ?A) ?\303)
  120.     ((?\~ ?C) ?\307)
  121.     ((?\~ ?D) ?\320)
  122.     ((?\~ ?N) ?\321)
  123.     ((?\~ ?O) ?\325)
  124.     ((?\~ ?a) ?\343)
  125.     ((?\~ ?c) ?\347)
  126.     ((?\~ ?d) ?\360)
  127.     ((?\~ ?n) ?\361)
  128.     ((?\~ ?o) ?\365)
  129.     ((?\~ ?>) ?\273)
  130.     ((?\~ ?<) ?\253)
  131.     ((?\/ ?A) ?\305) ;; A-with-ring (Norwegian and Danish)
  132.     ((?\/ ?E) ?\306) ;; AE-ligature (Norwegian and Danish)
  133.     ((?\/ ?O) ?\330)
  134.     ((?\/ ?a) ?\345) ;; a-with-ring (Norwegian and Danish)
  135.     ((?\/ ?e) ?\346) ;; ae-ligature (Norwegian and Danish)
  136.     ((?\/ ?o) ?\370)
  137.     )
  138.   "Association list for ISO accent combinations.")
  139.  
  140. (defvar iso-accents-minor-mode nil
  141.   "*Non-nil enables ISO Accents mode.
  142. Setting this variable makes it local to the current buffer.
  143. See `iso-accents-mode'.")
  144. (make-variable-buffer-local 'iso-accents-minor-mode)
  145.  
  146. (defun iso-accents-accent-key (prompt)
  147.   "Modify the following character by adding an accent to it."
  148.   ;; Pick up the accent character.
  149.   (if iso-accents-minor-mode
  150.       (iso-accents-compose prompt)
  151.     (char-to-string last-input-char)))
  152.  
  153. (defun iso-accents-compose-key (prompt)
  154.   "Modify the following character by adding an accent to it."
  155.   ;; Pick up the accent character.
  156.   (let ((combined (iso-accents-compose prompt)))
  157.     (if unread-command-events
  158.     (let ((unread unread-command-events))
  159.       (setq unread-command-events nil)
  160.       (error "Characters %s and %s cannot be composed"
  161.          (single-key-description (aref combined 0))
  162.          (single-key-description (car unread)))))
  163.     combined))
  164.  
  165. (defun iso-accents-compose (prompt)
  166.   (let* ((first-char last-input-char)
  167.      ;; Wait for the second key and look up the combination.
  168.      (second-char (if (or prompt
  169.                   (not (eq (key-binding "a")
  170.                        'self-insert-command)))
  171.               (progn
  172.                 (message "%s%c"
  173.                      (or prompt "Compose with ")
  174.                      first-char)
  175.                 (read-event))
  176.             (insert first-char)
  177.             (prog1 (read-event)
  178.               (delete-region (1- (point)) (point)))))
  179.      (entry (assoc (list first-char second-char) iso-accents-list)))
  180.     (if entry
  181.     ;; Found it: delete the first character and insert the combination.
  182.     (concat (list (nth 1 entry)))
  183.       ;; Otherwise, advance and schedule the second key for execution.
  184.       (setq unread-command-events (list second-char))
  185.       (vector first-char))))
  186.  
  187. (defvar iso-accents-enable '(?' ?` ?^ ?\" ?~ ?/)
  188.   "*List of accent keys that become prefixes in ISO Accents mode.
  189. The default is (?' ?` ?^ ?\" ?~ ?/), which contains all the supported
  190. accent keys.  For certain languages, you might want to remove some of
  191. those characters that are not actually used.")
  192.  
  193. (or key-translation-map (setq key-translation-map (make-sparse-keymap)))
  194. ;; For sequences starting with an accent character,
  195. ;; use a function that tests iso-accents-minor-mode.
  196. (if (memq ?' iso-accents-enable)
  197.     (define-key key-translation-map "'"  'iso-accents-accent-key))
  198. (if (memq ?` iso-accents-enable)
  199.     (define-key key-translation-map "`"  'iso-accents-accent-key))
  200. (if (memq ?^ iso-accents-enable)
  201.     (define-key key-translation-map "^"  'iso-accents-accent-key))
  202. (if (memq ?\" iso-accents-enable)
  203.     (define-key key-translation-map "\"" 'iso-accents-accent-key))
  204. (if (memq ?~ iso-accents-enable)
  205.     (define-key key-translation-map "~" 'iso-accents-accent-key))
  206. (if (memq ?/ iso-accents-enable)
  207.     (define-key key-translation-map "/" 'iso-accents-accent-key))
  208.  
  209. ;; It is a matter of taste if you want the minor mode indicated
  210. ;; in the mode line...
  211. ;; If so, uncomment the next four lines.
  212. ;; (or (assq 'iso-accents-minor-mode minor-mode-map-alist)
  213. ;;     (setq minor-mode-alist
  214. ;;       (append minor-mode-alist
  215. ;;           '((iso-accents-minor-mode " ISO-Acc")))))
  216.  
  217. ;;;###autoload
  218. (defun iso-accents-mode (&optional arg)
  219.   "Toggle ISO Accents mode, in which accents modify the following letter.
  220. This permits easy insertion of accented characters according to ISO-8859-1.
  221. When Iso-accents mode is enabled, accent character keys
  222. \(`, ', \", ^, / and ~) do not self-insert; instead, they modify the following
  223. letter key so that it inserts an ISO accented letter.
  224.  
  225. The variable `iso-accents-enable' specifies the list of characters to
  226. enable as accents.  If you don't need all of them, remove the ones you
  227. don't need from that list.
  228.  
  229. Special combinations: ~c gives a c with cedilla,
  230. ~d gives a d with dash.
  231. \"s gives German sharp s.
  232. /a gives a with ring.
  233. /e gives an a-e ligature.
  234. ~< and ~> give guillemets.
  235.  
  236. With an argument, a positive argument enables ISO Accents mode, 
  237. and a negative argument disables it."
  238.  
  239.   (interactive "P")
  240.  
  241.   (if (if arg
  242.       ;; Negative arg means switch it off.
  243.       (<= (prefix-numeric-value arg) 0)
  244.     ;; No arg means toggle.
  245.     iso-accents-minor-mode)
  246.       (setq iso-accents-minor-mode nil)
  247.  
  248.     ;; Enable electric accents.
  249.     (setq iso-accents-minor-mode t)))
  250.  
  251. ;;; iso-acc.el ends here
  252.